import { call, apply, delay, delay2, put, select, take, fork, cancel, takeEvery, all } from '../../redux-saga/effects'
// import { delay, cancel, takeEvery } from 'redux-saga/effects'
import { increase, actionTypes } from '../action/counter'
/**
 * 任务
 * @export
 */
export default function* (...args) {

    // const val = yield new Promise((resovle) => setTimeout(() => resovle(1), 1000))
    // console.log('promise result', val);

    // try {
    //     yield new Promise((resovle, reject) => setTimeout(() => reject(1), 1000))
    // } catch (err) {
    //     console.log('promise err result', err);
    // }

    // const val2 = yield 123
    // console.log('other result', val2);


    // call 处理异步函数
    // const val3 = yield call(asyncFun)
    // console.log('call result ', val3); // 1

    // apply
    // const val4 = yield apply(null, asyncFun)
    // console.log('apply result ', val4); // 1

    // delay
    // yield delay(2000)
    // console.log('delay result ');

    // delay
    // yield delay2(2000)
    // console.log('delay2 result ');

    // put 
    // yield put(increase())

    // select
    // const val5 = yield select()
    // const val5 = yield select(state => state.counter)
    // console.log('select result ', val5);

    // take 
    // 监听asyncDecrease分发，返回一个完整的action对象
    // const action = yield take(actionTypes.asyncIncrease)
    // console.log('action', action);

    // fork 开启一个新的任务，且不会阻塞
    // const task = yield fork(forkTask)

    // cancel 取消任务
    // yield delay(3000)
    // yield cancel(task)
    // console.log('fork 任务被取消');

    // takeEvery
    // const task = yield takeEvery(actionTypes.asyncIncrease, allGen2)
    // yield delay(5000)
    // yield cancel(task)
    // console.log('取消 takeEvery', task);

    // all
    // yield all([allGen1, allGen2])
    // console.log('all');
}

function* allGen1() {
    yield delay(1000)
    console.log('---allGen1---');
}

function* allGen2() {
    yield delay(1000)
    console.log('---allGen1---');
}

function* forkTask() {
    while (true) {
        yield delay(1000)
        console.log('---forTask---');
    }
}


function asyncFun() {
    return new Promise((resovle) => setTimeout(() => resovle(1), 1000))
}